package com.modificial.shiro.system.dao;

import com.modificial.shiro.mapper.BaseMapper;
import com.modificial.shiro.system.domain.SysRole;
import com.modificial.shiro.system.domain.SysUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author modificial
 * @date 2018/8/4 0004
 * @company modificial_org
 * @description 用户操作
 */
@Repository
public interface UserDao extends BaseMapper<SysUser> {
    /**
     * 查询用户信息根据用户名字
     * @param name 用户名
     * @return
     */
    @Select("select user_id,user_name,password,salt,is_enable from sys_user where user_name=#{name}")
    SysUser findUserByName(String name);

    /**
     * 禁用或启用用户
     * @param userId 用户id
     * @param isEnable 0禁用 1启用
     * @return
     */
    @Update("update sys_user set is_enable=#{isEnable} where user_id=#{userId}")
    int disableUser(@Param("userId") String userId, @Param("isEnable") Integer isEnable);

    /**
     * 更新用户
     * @param sysUser
     * @return
     */
    @Update("update sys_user set password=#{password},user_phone=#{userPhone},update_time=now() where user_id=#{userId}")
    int updateUser(SysUser sysUser);

    /**
     * 根据用户查询角色
     * @param userId 用户id
     * @return
     */
    @Select("select T1.role_name,T1.role_id from sys_role T1 left join sys_user_role T2 " +
            "on T1.role_id=T2.role_id and T2.user_id=#{userId} and T1.is_enable=1")
    List<SysRole> findRolesByUser(String userId);
}
